// pages/backlog/backlog.js
let app = getApp();
import { getTicketListApi, buyTicketsApi } from "../../api/index.js";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    pageAllHeight: app.globalData.pageAllHeight - 430,
    parkValue: "请选择车场名称",
    parkColumns: [{ text: "全部车场", value: null }],
    parkId: null,
    ticketType: null,
    ticketStatus: null,
    selected: 0,
    selectedData: {},
    ticketList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},

  onShow() {
    this.init();
  },
  //获取停车场列表
  init() {
    let parkId = wx.getStorageSync("parkId");
    if(parkId) {
      this.getTicketList(parkId);
    }
  },

  //通行券列表
  async getTicketList(parkId) {
    let params = {
      parkId,
      ticketType: this.data.ticketType,
      ticketStatus: this.data.ticketStatus,
    };
    const { data: res } = await getTicketListApi(params);
    this.setData({
      ticketList: res.data,
    });
  },
  //   修改通行券
  editTicket(e) {
    const { item } = e.currentTarget.dataset;
    const { type } = e.currentTarget.dataset;
    wx.navigateTo({
      url:
        "/pages/parkCoupon/createCoupon/createCoupon?item=" +
        JSON.stringify(item) +
        "&type=" +
        type,
    });
  },

  // 选中券
  selectItem: function (e) {
    this.setData({
      selected: e.detail,
    });
  },
  // 跳转页面
  toPage(e) {
    console.log(e);
    let url = e.target.dataset.url;
    wx.navigateTo({
      url: url,
    });
  },
  //出售车场券
  async sellParkTicket() {
    try {
      const { selected, ticketList } = this.data;
      const { gid, ticketPrice, discountRate } = ticketList.respList[selected];
      console.log(ticketList.discountFund);
      if (ticketList.discountFund > 0) {
        getApp().showToast("余额充足！");
        return;
      }
      const params = {
        gid,
        parkId: wx.getStorageSync("parkId"),
        ticketType: 1,
        ticketPrice,
        discountRate,
      };
      await buyTicketsApi(params);
      wx.showModal({
        title: "提示",
        content: "出售成功",
        confirmText: "查看明细",
        complete: (res) => {
          if (res.confirm) {
            wx.navigateTo({
              url: "/pages/salesDetails/salesDetails?gid=" + gid,
            });
          }
        },
      });
    } catch (error) {
      console.error(error);
    }
  },
});
